package com.haotunet.app.goldtreasure.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.model.CurrentBuyRecord;
import com.haotunet.app.goldtreasure.views.TipPopupView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static android.R.attr.type;


public class BuyRecordAdapter extends HolderAdapter<CurrentBuyRecord, BuyRecordAdapter.ViewHolder> {

    public BuyRecordAdapter(Context context, List<CurrentBuyRecord> mlist, int type) {
        super(context, mlist, type);
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, CurrentBuyRecord t, int position) {
        return inflate(R.layout.item_buy_record);
    }

    @Override
    public ViewHolder buildHolder(View convertView, CurrentBuyRecord t, int position) {
        ViewHolder holder = new ViewHolder(convertView);
        return holder;
    }

    @Override
    public void bindViewDatas(View convertView, ViewHolder holder, CurrentBuyRecord currentBuyRecord, int position) {
        holder.productName.setText(currentBuyRecord.getProductName());
        if (!TextUtils.isEmpty(currentBuyRecord.getProductName()) && currentBuyRecord.getProductName().length() > 6) {
            holder.productName.setTextSize(11);
        } else {
            holder.productName.setTextSize(13);
        }
        holder.buyNumber.setText(currentBuyRecord.getQuantity());
        holder.payAmount.setText(currentBuyRecord.getPayment());
        if (type == 3) {
            holder.storeDays.setText(currentBuyRecord.getTrustPeriod());
            holder.unitPrice.setText(currentBuyRecord.getPurchasePrice());
        } else {
            holder.unitPrice.setText(currentBuyRecord.getPrice());
            holder.storeDays.setText(currentBuyRecord.getOrderStatus());
        }
        if (type == 3 || ("0元".equals(currentBuyRecord.getDiscount()) && TextUtils.isEmpty(currentBuyRecord.getRedPacketPay()))) {
            holder.llDiscountLayout.setVisibility(View.GONE);
        } else {
            holder.llDiscountLayout.setVisibility(View.VISIBLE);
            if ("0元".equals(currentBuyRecord.getDiscount())) {
                holder.txtDiscountMoney.setVisibility(View.GONE);
            } else {
                holder.txtDiscountMoney.setVisibility(View.VISIBLE);
                holder.txtDiscountMoney.setText("已优惠" + currentBuyRecord.getDiscount());
            }
            if (TextUtils.isEmpty(currentBuyRecord.getRedPacketPay())) {
                holder.txtRedPacketMoney.setVisibility(View.GONE);
            } else {
                holder.txtRedPacketMoney.setVisibility(View.VISIBLE);
                holder.txtRedPacketMoney.setText(currentBuyRecord.getRedPacketPay());
            }
        }
        if (!TextUtils.isEmpty(currentBuyRecord.getRemark())) {
            holder.experienceGoldMessageLayout.setVisibility(View.VISIBLE);
            holder.txtRemark.setText(currentBuyRecord.getRemark());
        }else{
            holder.experienceGoldMessageLayout.setVisibility(View.GONE);
        }
        final String tips = currentBuyRecord.getTips();
        if (!TextUtils.isEmpty(tips)) {
            holder.iv_tip.setVisibility(View.VISIBLE);
            holder.buyNumberLayout.setEnabled(true);
        } else {
            holder.iv_tip.setVisibility(View.GONE);
            holder.buyNumberLayout.setEnabled(false);
        }
        holder.txtCurrentOrderNo.setText(currentBuyRecord.getOrderNo());
        holder.txtBuyTime.setText(currentBuyRecord.getCreateTime());
        holder.buyNumberLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TipPopupView(context, null, tips, R.style.MyDialog).show();
            }
        });
    }

    static class ViewHolder {
        @InjectView(R.id.product_name)
        TextView productName;
        @InjectView(R.id.unit_price)
        TextView unitPrice;
        @InjectView(R.id.buy_number)
        TextView buyNumber;
        @InjectView(R.id.buy_number_layout)
        LinearLayout buyNumberLayout;
        @InjectView(R.id.pay_amount)
        TextView payAmount;
        @InjectView(R.id.txt_discount_money)
        TextView txtDiscountMoney;
        @InjectView(R.id.txt_red_packet_money)
        TextView txtRedPacketMoney;
        @InjectView(R.id.txt_remark)
        TextView txtRemark;
        @InjectView(R.id.experience_gold_message_layout)
        LinearLayout experienceGoldMessageLayout;
        @InjectView(R.id.ll_discount_layout)
        LinearLayout llDiscountLayout;
        @InjectView(R.id.store_days)
        TextView storeDays;
        @InjectView(R.id.txt_current_order_no)
        TextView txtCurrentOrderNo;
        @InjectView(R.id.txt_buy_time)
        TextView txtBuyTime;
        @InjectView(R.id.icon_tip)
        ImageView iv_tip;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

    public static boolean isOverFlowed(TextView tv) {
        int availableWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight();
        Paint textViewPaint = tv.getPaint();
        float textWidth = textViewPaint.measureText(tv.getText().toString());
        if (textWidth > availableWidth) {
            return true;
        } else {
            return false;
        }
    }
}
